<?php
namespace app\index\controller;

use app\common\controller\FrontBase;
use app\common\service\Post as PostService;
use app\common\service\PostClass as PostClassService;
use app\common\tool\Tool;
use think\Request;

class Post extends  FrontBase{

    /**
     * 文章列表
     * @return \think\response\View
     */
    public function index(Request $request){
        $id = $request->param('id');
        if(!intval($id)) $id = 0;
        //找到文章的栏目
        $postClassService = new PostClassService();
        $postClasses = $postClassService -> paginate();
        $postClasses = collection($postClasses->items())->toArray();
        //当前分类下文章
        $this->param['postClasses'] = $postClasses;

        $postService = new PostService();
        if(!$id) {
            $pcIds = [];
            foreach ($postClasses as $key => $val) {
                array_push($pcIds, $val['id']);
            }
            $posts = $postService->paginateByPc($pcIds);
        }else{
            $posts = $postService->paginateByPc([$id]);
        }
        $this->param['posts'] = $posts;

        //最新新闻
        $newestPosts = $postService ->paginate(5);
        $this->param['newsPosts'] = $newestPosts;

        return view('',$this->param);
    }

    public function post(Request $request){
        $id = $request->param('id');
        $postClassService = new PostClassService();
        $postClasses = $postClassService -> paginate();
        $postClasses = collection($postClasses->items())->toArray();
        //当前分类下文章
        $this->param['postClasses'] = $postClasses;

        //最新新闻
        $postService = new PostService();
        $newestPosts = $postService ->paginate(5);
        $this->param['newsPosts'] = $newestPosts;

        $post = $postService->find($id);
        $this->param['post'] = $post;

        $postService->addPv($id);

        return view('',$this->param);
    }
}
